package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.*;
import com.itheima.utils.DownloadUtil;
import com.itheima.web.controller.BaseController;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

@Controller
@RequestMapping("/cargo/invoice")
public class InvoiceController extends BaseController {

    @Reference
    private InvoiceService invoiceService;

    @Reference
    private PackingListService packingListService;

    @Reference
    private ExportService exportService;

    @Reference
    private ContractService contractService;

    @Reference
    private ShippingOrderService shippingOrderService;

    @RequestMapping(value = "/list", name = "发票列表查询")
    public String contractList(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {

        InvoiceExample invoiceExample = new InvoiceExample();
        PageInfo pageInfo = invoiceService.findByPage(pageNum, pageSize, invoiceExample);
        request.setAttribute("page", pageInfo);
        return "cargo/invoice/invoice-list";
    }

    @RequestMapping(value = "/toAdd", name = "跳转发票添加页面")
    public String toAdd(String id) {

        PackingList packingList = packingListService.selectByPrimaryKey(id);
        String invoiceNo = packingList.getInvoiceNo();

        String exportIds = packingList.getExportIds();
        String[] split = exportIds.split(",");
        Export export = exportService.findById(split[0]);

        String contractIds = export.getContractIds();
        String[] split1 = contractIds.split(",");
        Contract contract = contractService.findById(split1[0]);

        request.setAttribute("invoiceNo", invoiceNo);
        request.setAttribute("invoiceId", id);
        request.setAttribute("contract", contract);
        return "cargo/invoice/invoice-add";
    }

    @RequestMapping(value = "/add", name = "发票添加")
    public String add(Invoice invoice) {

        invoice.setCreateBy(getUser().getUserName());
        invoice.setCreateDept(getUser().getDeptId());
        invoice.setCreateTime(new Date());

        invoiceService.save(invoice);

        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转发票修改页面")
    public String toUpdate(String id) {

        Invoice invoice = invoiceService.findById(id);
        request.setAttribute("invoice", invoice);

        return "cargo/invoice/invoice-update";
    }

    @RequestMapping(value = "/edit", name = "发票修改")
    public String edit(Invoice invoice) {

        invoice.setCreateBy(getUser().getUserName());
        invoice.setCreateDept(getUser().getDeptId());

        invoiceService.update(invoice);

        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除发票")
    public String delete(String id) {
        invoiceService.delete(id);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/printExcelwithTemplate", name = "发票导出--使用模板")
    public void printExcelwithTemplate(String id) throws IOException {

        PackingList packingList = packingListService.selectByPrimaryKey(id);
        Invoice invoice = invoiceService.findById(id);
        ShippingOrder shippingOrder = shippingOrderService.findById(id);

        //2 封装execl
        //2-1) 读取模板文件---工作簿
        String realPath = session.getServletContext().getRealPath("/make/xlsprint/template02.xlsx");
        Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(realPath)));

        Sheet sheet = workbook.getSheetAt(0);

        Row row3 = sheet.createRow(2);
        row3.createCell(0).setCellValue(packingList.getSeller());
        row3.createCell(1).setCellValue(packingList.getBuyer());

        Row row5 = sheet.createRow(4);
        row5.createCell(0).setCellValue(invoice.getInvoiceId());
        row5.createCell(1).setCellValue(invoice.getCreateTime());
        row5.createCell(2).setCellValue(invoice.getScNo());
        row5.createCell(3).setCellValue(invoice.getBlNo());

        Row row7 = sheet.createRow(6);
        row7.createCell(0).setCellValue(invoice.getTradeTerms());
        row7.createCell(1).setCellValue(shippingOrder.getPortOfLoading());
        row7.createCell(2).setCellValue(shippingOrder.getPortOfDischarge());

        Row row9 = sheet.createRow(8);
        Integer price = new Random().nextInt(1000);
        row9.createCell(0).setCellValue(packingList.getTotalCartons());
        row9.createCell(1).setCellValue(price);
        row9.createCell(2).setCellValue(packingList.getTotalCartons() * price * 1.17);


        //3 下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtil.download(byteArrayOutputStream, response, "发票" + id + ".xlsx");
    }

    @RequestMapping(value = "/exportE", name = "发票校验")
    public String exportE(String id) {

        Invoice invoice = invoiceService.findById(id);
        invoice.setState(1);
        invoiceService.update(invoice);

        return "redirect:/cargo/invoice/list.do";
    }
}
